<?php
namespace app\index\controller;
use think\Db;
use think\Config;
class Index extends Base
{
    public function index()
    {
        Db::table('up_active')
            ->where('active_end_time','<=',date('Y-m-d H:i:s'))
            ->where('active_status',1)
            ->setField('active_status',2);
        Db::table('up_active')
            ->where('active_end_time','>=',date('Y-m-d H:i:s'))
            ->where('active_status',2)
            ->setField('active_status',1);
        $data = Db::table('up_active')
            ->order('active_start_time desc')
            ->page(1,10)
            ->select();
        $this->assign('list',$data);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * ajax请求
     */
    public function ajaxactivitylist(){
        extract(input());
        $data = Db::table('up_active')
            ->order('active_start_time desc')
            ->page($page,10)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /***
     * @return \think\response\Json
     * 活动详情
     */
    public function activitydatail(){
        extract(input());
        $user = $this->getUserInfo();
        $data = Db::table('up_active')
            ->where('active_id',$activity_id)
            ->find();
        $have_data = Db::table('up_active_member')
            ->where('am_user_id',$user['user_id'])
            ->where('am_active_id',$activity_id)
            ->find();
        $have = 0;
        if($have_data){
            $have = 1;
        }
        $this->assign('have',$have);
        $this->assign('have_data',$have_data);
        $this->assign('list',$data);
        //
        $have_num = Db::table('up_active_member')
            ->where('am_user_status','in','1,2')
            ->where('am_active_id',$activity_id)
            ->count();
        $this->assign('have_num',$have_num);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 填写报名信息
     */
    public function baoming(){
        extract(input());
        $num = Db::table('up_active')
            ->where('active_id',$aid)
            ->value('active_member_num');
        $have = Db::table('up_active_member')
            ->where('am_user_status','in','1,2')
            ->where('am_active_id',$aid)
            ->count();
        if($have >= $num){
            echo "<script> alert('已超出报名人数，可稍后再试，等待其他用户审核结果！'); </script>";
        }else{
            $this->assign('aid',$aid);
            return $this->fetch();
        }
    }
    /**
     * 提交报名
     * @return string
     */
    public function ajaxBaoming(){
        $phone = trim($_POST['phone'],' ');
        $name = trim($_POST['name'],' ');
        $company = trim($_POST['company'],' ');
        $aid = trim($_POST['aid'],' ');
        if($aid == ""){
            return json(['status'=>0,'msg'=>'未知错误，请重新进入']);
        }
        if($name == ""){
            return json(['status'=>0,'msg'=>'姓名不能为空']);
        }
        if($phone == ""){
            return json(['status'=>0,'msg'=>'手机号不能为空']);
        }
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json(['status'=>0,'msg'=>'请填写正确的手机号码']);
        }
        if($company == ""){
            return json(['status'=>0,'msg'=>'公司名称不能为空']);
        }
        //=============================================================
        $user = $this->getUserInfo();
        $have_data = Db::table('up_active_member')
            ->where('am_user_id',$user['user_id'])
            ->where('am_active_id',$aid)
            ->find();
        if($have_data){
            if($have_data['am_user_status'] == 3){
                $res = Db::table('up_active_member')
                    ->where('am_user_id',$user['user_id'])
                    ->where('am_active_id',$aid)
                    ->setField('am_user_status',1);
                if($res){
                    Db::table('up_active')
                        ->where('active_id',$aid)
                        ->setInc('active_member_num');
                    return json(['status'=>200,'msg'=>'报名成功']);
                }else{
                    return json(['status'=>0,'msg'=>'报名失败，请刷新重试']);
                }
            }else{
                return json(['status'=>0,'msg'=>'您已经报过名了']);
            }
        }else{
            $ercode = $this->scerweima($aid,$user['user_id']);
            $insert = [
                'am_user_id' => $user['user_id'],
                'am_active_id' => $aid,
                'am_user_name' => $name,
                'am_user_phone' => $phone,
                'am_user_company' => $company,
                'am_ercode' => $ercode,
                'am_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_active_member')->insert($insert);
            if($res){
                Db::table('up_active')
                    ->where('active_id',$aid)
                    ->setInc('active_member_num');
                return json(['status'=>200,'msg'=>'报名成功']);
            }else{
                return json(['status'=>0,'msg'=>'报名失败，请刷新重试']);
            }
        }
    }
    //生成二维码图片
    function scerweima($aid,$uid){
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        require_once VENDOR_PATH . "phpqrcode.php";
        $rand = date('YmdHis').rand(100000,999999);
        $value = "$url/index/Index/hexiao?aid=$aid&uid=$uid"; //二维码内容：链接
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 8;           //生成图片大小
        //生成二维码图片
        $ymd = date('Ymd');
        $zip_path = "{$dir}/uploads/face/$ymd";
        if (!file_exists($zip_path)) {
            mkdir($zip_path);
        }
        $filename = "$zip_path/{$rand}.png";
        $filehttp = "{$url}/uploads/face/$ymd/{$rand}.png";
        \QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
        $QR = $filename;              //已经生成的原始二维码图片文件
        $QR = imagecreatefromstring(file_get_contents($QR));
        //输出图片
//        imagepng($QR, 'qrcode.png');
        imagedestroy($QR);
        return $filehttp;
    }
    public function hexiao(){
        //核销资格
        $user = $this->getUserInfo();
        if($user['user_auth'] == 2){
            $aid = $_GET['aid'];
            $uid = $_GET['uid'];
            $have_data = Db::table('up_active_member')
                ->where('am_user_id',$uid)
                ->where('am_active_id',$aid)
                ->find();
            if($have_data){
                if($have_data['am_status'] == 2){
                    echo "此二维码已经核销过了";
                }else{
                    $res = Db::table('up_active_member')
                        ->where('am_user_id',$uid)
                        ->where('am_active_id',$aid)
                        ->setField('am_status',2);
                    if($res){
                        $this->assign('data',$have_data);
                        return $this->fetch('index/hexiaouser');
                    }else{
                        echo "核销失败，请刷新重试！";
                    }
                }
            }else{
                echo "未获取到此二维码信息，请核实！";
            }
        }else{
            echo "您没有核销资格！";
        }

    }
    public function hexiaoself(){
        //核销资格
        $user = $this->getUserInfo();
        $aid = $_GET['aid'];
        $uid = $user['user_id'];
        $have_data = Db::table('up_active_member')
            ->where('am_user_id',$uid)
            ->where('am_active_id',$aid)
            ->find();
        if($have_data){
            $res = Db::table('up_active_member')
                ->where('am_user_id',$uid)
                ->where('am_active_id',$aid)
                ->setField('am_status',2);
            $this->assign('data',$have_data);
            return $this->fetch('index/hexiaouser');
        }else{
            echo "未获取到此二维码信息，请核实！";
        }

    }
//    public function hexiaouser(){
//        extract(input());
//        $have_data = Db::table('up_active_member')
//            ->where('am_id',$am_id)
//            ->find();
//        $this->assign('data',$have_data);
//        return $this->fetch();
//    }
}
